package org.shopen.puremvc.view
{
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.TheoryProxy;
	import org.shopen.puremvc.model.domain.theory.TheoryMeta;
	import org.shopen.puremvc.view.components.theory.ContentAccordion;
	import org.shopen.puremvc.view.components.theory.ContentItem;

	public class ContentAccordionMediator extends Mediator
	{
		public static const NAME : String = "ContentAccordionMediator";
		
		protected function get accordion() : ContentAccordion
		{
			var result : ContentAccordion = viewComponent as ContentAccordion;
			return result;
		}
		
		public function ContentAccordionMediator(viewComponent:Object=null)
		{
			super(ContentAccordionMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			sendNotification(ApplicationConstants.LOAD_THEORY_META);
		}
		
		override public function getMediatorName():String
		{
			return ContentAccordionMediator.NAME;
		}
		
		override public function listNotificationInterests():Array
		{
			return [TheoryProxy.THEORY_META_INITIALIZED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var noteName : String = notification.getName();
			switch (noteName)
			{
				case TheoryProxy.THEORY_META_INITIALIZED:
					var metas : ArrayCollection = notification.getBody() as ArrayCollection;
					initContent(metas);
					break;
			}
		}
		
		protected function initContent(metas : ArrayCollection) : void
		{
			clearAccordion();
			for each(var meta : TheoryMeta in metas)
			{
				var contentItem : ContentItem = new ContentItem();
				accordion.addChild(contentItem);
				if (facade.hasMediator(ContentItemMediator.NAME + meta.id))
				{
					facade.removeMediator(ContentItemMediator.NAME + meta.id);
				}
				facade.registerMediator(new ContentItemMediator(contentItem, meta));
				sendNotification(ApplicationConstants.INIT_THEORY, null, meta.id);
			}
			
		}
		
		protected function clearAccordion() : void
		{
			accordion.removeAllChildren();
		}
		
		
	}
}